package com.hall.controller;

import com.hall.pojo.Appointment;
import com.hall.pojo.SysUser;
import com.hall.pojo.SysUserVo;
import com.hall.pojo.UserLogin;
import com.hall.pojo.common.Result;
import com.hall.service.MyEditorServes;
import com.hall.service.MyEditorServesImpl;
import com.hall.service.UserService;
import com.hall.utils.Sendsms;
import com.hall.utils.UserSession;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.UUID;

/**
 * 用户信息修改
 */
@Controller
public class MyEditorController {
    @Value("${userfiles}")
    private String filePath;
    @Autowired
    MyEditorServes myEditorServes;
    @Autowired
    UserService userService;
    private String userid;

    //修改用户信息的页面
    @RequestMapping("/my/editorInformation")
    public String editorList(HttpServletRequest request, Model model, HttpSession session) {
        SysUser sysUser = UserSession.getSysUser(request);
        System.out.println("文件路径为++++++++++++++++++++" + filePath);
        String id = sysUser.getId();
        userid = id;  //声明id为成员变量并赋值
        SysUser sysuser1 = myEditorServes.findAllUserById(id);
        System.out.println("+++++++++++++++++++++++++" + sysuser1.getLoginName());
        model.addAttribute("sysuser", sysuser1);
        return "editorInformation";
    }

    //上传头像方法
    @ResponseBody
    @RequestMapping("/my/uploadImage")
    public Result upload(MultipartFile file,String id,HttpServletRequest request) throws IOException {
        SysUser sysUser=new SysUser();
        String fileName = file.getOriginalFilename();//获取图片名
        String suffix = fileName.substring(fileName.lastIndexOf("."));//获取图片文件名后缀
        System.out.println("后缀名=" + suffix);
        //String path2 = request.getSession().getServletContext().getRealPath("/static/img");//获取部署到服务器tomcat后的资源路径(图片将要上传的路径):服务器路径
        String path2 = filePath;
        fileName = UUID.randomUUID() + suffix;  //给文件生成uuid
        System.out.println(fileName);
        String path = DateTimeFormatter.ofPattern("yyyy/MM/dd") //添加日期文件夹路径到数据库图片名称前
                .format(LocalDate.now());
        String dirPath = path2 + "/" + path;
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String filePath = dirPath + "/" + fileName;
        file.transferTo(new File(filePath));
        sysUser.setPhoto("/" + path + "/" + fileName);
        SysUser sysUser2 = new SysUser();
        sysUser2.setId(id);
        sysUser2.setPhoto(sysUser.getPhoto());
        int set = myEditorServes.upLoadImg(sysUser2);  //生成的文件名存入路径
        HashMap<String,String>map= new HashMap<>();
        map.put("url",sysUser.getPhoto());
        map.put("filename",fileName);
        if(set<1){
            return Result.error("修改头像失败,请重试");
        }
        return Result.success(map,"修改头像成功");
    }

    //修改个人信息方法
    @ResponseBody
    @RequestMapping("/my/updateOne")
    public Result updateUserInfo(SysUserVo sysUserVo) {
        SysUser s3= userService.getUser(sysUserVo.getId());
        if(sysUserVo.getId()==null || sysUserVo.getId().isEmpty()){
            return Result.error("当前id不能为空");
        }
        if(s3==null){
            return Result.error("当前id不存在");
        }
        //验证手机号是否注册
        String nowUserPhone = s3.getPhone();//当前用户手机号
        String insertPhone = sysUserVo.getPhone(); //提交的手机号
        if(!(insertPhone==null || insertPhone.equals(""))){
            if (!insertPhone.equals(nowUserPhone)) {   //如果提交的手机号
                boolean c = userService.checkLoginByPhone(new UserLogin().setPhone(insertPhone)) != null;
                if (c) {
                    return Result.error("修改失败,当前手机号已被注册");
                }
            }
        }
        /*创建了vo类来接收表单*/
        SysUser sysUser3 = new SysUser();
        sysUser3.setId(sysUserVo.getId());
        sysUser3.setUsertype(sysUserVo.getUsertype());
        sysUser3.setCompanyId(sysUserVo.getCompanyId());
        sysUser3.setOfficeId(sysUserVo.getOfficeId());
        sysUser3.setLevel(sysUserVo.getLevel());
        sysUser3.setMajor(sysUserVo.getMajor());
        sysUser3.setResume(sysUserVo.getResume());
        sysUser3.setName(sysUserVo.getName());
        sysUser3.setPhone(sysUserVo.getPhone());
        sysUser3.setWechat(sysUserVo.getWechat());
        sysUser3.setQrcode(sysUserVo.getQrcode());
        int i = myEditorServes.updateOne(sysUser3);
        if (i != 0) {
            return Result.success("id:"+sysUser3.getId(), "修改成功");
        } else {
            return Result.error("修改失败");
        }
    }
}
